﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;

namespace DataLib.DAL
{
    /// <summary>
    /// 数据访问类:InStorage
    /// </summary>
    public partial class InStorage
    {
        public InStorage()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "InStorage");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from InStorage");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DataLib.Model.InStorage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into InStorage(");
            strSql.Append("cjoid,wrlno,instoragetime,outid,outno,Operator,InDepart,OutDepart,status,islock,instotal,loadnumid,mechanerr,vloadnum,clearingmonth,flno,shno,flwt,shwt,remark)");
            strSql.Append(" values (");
            strSql.Append("@cjoid,@wrlno,@instoragetime,@outid,@outno,@Operator,@InDepart,@OutDepart,1,@islock,@instotal,@loadnumid,@mechanerr,@vloadnum,@clearingmonth,@flno,@shno,@flwt,@shwt,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@cjoid", SqlDbType.Int,4),
					new SqlParameter("@wrlno", SqlDbType.NVarChar,30),
					new SqlParameter("@instoragetime", SqlDbType.DateTime),
					new SqlParameter("@outid", SqlDbType.Int,4),
					new SqlParameter("@outno", SqlDbType.NVarChar,30),
					new SqlParameter("@Operator", SqlDbType.NVarChar,30),
					new SqlParameter("@InDepart", SqlDbType.NVarChar,30),
					new SqlParameter("@OutDepart", SqlDbType.NVarChar,30),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@islock", SqlDbType.Bit,1),
					new SqlParameter("@instotal", SqlDbType.Decimal,9),
					new SqlParameter("@loadnumid", SqlDbType.Int,4),
					new SqlParameter("@mechanerr", SqlDbType.Decimal,9),
                    new SqlParameter("@vloadnum",SqlDbType.VarChar,12),
                    new SqlParameter("@clearingmonth",SqlDbType.VarChar,6),
					new SqlParameter("@flno", SqlDbType.VarChar,20),
					new SqlParameter("@shno", SqlDbType.VarChar,20),
                    new SqlParameter("@flwt",SqlDbType.Decimal,12),
                    new SqlParameter("@shwt",SqlDbType.Decimal,12),
                    new SqlParameter("@remark",SqlDbType.VarChar,300)
                                        };
            parameters[0].Value = model.cjoid;
            parameters[1].Value = model.wrlno;
            parameters[2].Value = model.instoragetime;
            parameters[3].Value = model.outid;
            parameters[4].Value = model.outno;
            parameters[5].Value = model.Operator;
            parameters[6].Value = model.InDepart;
            parameters[7].Value = model.OutDepart;
            parameters[8].Value = model.status;
            parameters[9].Value = model.islock;
            parameters[10].Value = model.instotal;
            parameters[11].Value = model.loadnumid;
            parameters[12].Value = model.mechanerr;
            parameters[13].Value = model.vloadnum;
            parameters[14].Value = model.clearingmonth;
            parameters[15].Value = model.flno;
            parameters[16].Value = model.shno;
            parameters[17].Value = model.flwt;
            parameters[18].Value = model.shwt;
            parameters[19].Value = model.remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DataLib.Model.InStorage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update InStorage set ");
            strSql.Append("cjoid=@cjoid,");
            strSql.Append("wrlno=@wrlno,");
            strSql.Append("instoragetime=@instoragetime,");
            strSql.Append("outid=@outid,");
            strSql.Append("outno=@outno,");
            strSql.Append("Operator=@Operator,");
            strSql.Append("InDepart=@InDepart,");
            strSql.Append("OutDepart=@OutDepart,");
            strSql.Append("status=@status,");
            strSql.Append("islock=@islock,");
            strSql.Append("instotal=@instotal,");
            strSql.Append("loadnumid=@loadnumid,");
            strSql.Append("mechanerr=@mechanerr,");
            strSql.Append("vloadnum=@vloadnum,");
            strSql.Append("clearingmonth=@clearingmonth,");
            strSql.Append("flno=@flno,");
            strSql.Append("shno=@shno,");
            strSql.Append("flwt=@flwt,");
            strSql.Append("remark=@remark,");
            strSql.Append("shwt=@shwt");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@cjoid", SqlDbType.Int,4),
					new SqlParameter("@wrlno", SqlDbType.NVarChar,30),
					new SqlParameter("@instoragetime", SqlDbType.DateTime),
					new SqlParameter("@outid", SqlDbType.Int,4),
					new SqlParameter("@outno", SqlDbType.NVarChar,30),
					new SqlParameter("@Operator", SqlDbType.NVarChar,30),
					new SqlParameter("@InDepart", SqlDbType.NVarChar,30),
					new SqlParameter("@OutDepart", SqlDbType.NVarChar,30),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@islock", SqlDbType.Bit,1),
					new SqlParameter("@instotal", SqlDbType.Decimal,9),
					new SqlParameter("@loadnumid", SqlDbType.Int,4),
					new SqlParameter("@mechanerr", SqlDbType.Decimal,9),
                    new SqlParameter("@vloadnum",SqlDbType.VarChar,12),
                    new SqlParameter("@clearingmonth",SqlDbType.VarChar,6),
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@flno", SqlDbType.VarChar,20),
					new SqlParameter("@shno", SqlDbType.VarChar,20),
                    new SqlParameter("@flwt",SqlDbType.Decimal,12),
                    new SqlParameter("@shwt",SqlDbType.Decimal,12),
                    new SqlParameter("@remark", SqlDbType.VarChar,300)};
            parameters[0].Value = model.cjoid;
            parameters[1].Value = model.wrlno;
            parameters[2].Value = model.instoragetime;
            parameters[3].Value = model.outid;
            parameters[4].Value = model.outno;
            parameters[5].Value = model.Operator;
            parameters[6].Value = model.InDepart;
            parameters[7].Value = model.OutDepart;
            parameters[8].Value = model.status;
            parameters[9].Value = model.islock;
            parameters[10].Value = model.instotal;
            parameters[11].Value = model.loadnumid;
            parameters[12].Value = model.mechanerr;
            parameters[13].Value = model.vloadnum;
            parameters[14].Value = model.clearingmonth;
            parameters[15].Value = model.id;
            parameters[16].Value = model.flno;
            parameters[17].Value = model.shno;
            parameters[18].Value = model.flwt;
            parameters[19].Value = model.shwt;
            parameters[20].Value = model.remark;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from InStorage ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from InStorage ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.InStorage GetModel(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,cjoid,wrlno,instoragetime,outid,outno,Operator,InDepart,OutDepart,status,islock,instotal,loadnumid,mechanerr,vloadnum,clearingmonth,flno,shno,flwt,shwt,remark from InStorage ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DataLib.Model.InStorage model = new DataLib.Model.InStorage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.InStorage DataRowToModel(DataRow row)
        {
            DataLib.Model.InStorage model = new DataLib.Model.InStorage();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["cjoid"] != null && row["cjoid"].ToString() != "")
                {
                    model.cjoid = int.Parse(row["cjoid"].ToString());
                }
                if (row["wrlno"] != null)
                {
                    model.wrlno = row["wrlno"].ToString();
                }
                if (row["instoragetime"] != null && row["instoragetime"].ToString() != "")
                {
                    model.instoragetime = DateTime.Parse(row["instoragetime"].ToString());
                }
                if (row["outid"] != null && row["outid"].ToString() != "")
                {
                    model.outid = int.Parse(row["outid"].ToString());
                }
                if (row["outno"] != null)
                {
                    model.outno = row["outno"].ToString();
                }
                if (row["Operator"] != null)
                {
                    model.Operator = row["Operator"].ToString();
                }
                if (row["InDepart"] != null)
                {
                    model.InDepart = row["InDepart"].ToString();
                }
                if (row["OutDepart"] != null)
                {
                    model.OutDepart = row["OutDepart"].ToString();
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["islock"] != null && row["islock"].ToString() != "")
                {
                    if ((row["islock"].ToString() == "1") || (row["islock"].ToString().ToLower() == "true"))
                    {
                        model.islock = true;
                    }
                    else
                    {
                        model.islock = false;
                    }
                }
                if (row["instotal"] != null && row["instotal"].ToString() != "")
                {
                    model.instotal = decimal.Parse(row["instotal"].ToString());
                }
                if (row["loadnumid"] != null && row["loadnumid"].ToString() != "")
                {
                    model.loadnumid = int.Parse(row["loadnumid"].ToString());
                }
                if (row["mechanerr"] != null && row["mechanerr"].ToString() != "")
                {
                    model.mechanerr = decimal.Parse(row["mechanerr"].ToString());
                }
                if (row["vloadnum"] != null && row["vloadnum"].ToString() != "")
                {
                    model.vloadnum = row["vloadnum"].ToString();
                }
                if (row["clearingmonth"] != null && row["clearingmonth"].ToString() != "")
                {
                    model.clearingmonth = row["clearingmonth"].ToString();
                }
                if (row["flno"] != null && row["flno"].ToString().Trim()!="")
                {
                    model.flno = row["flno"].ToString().Trim();
                }
                if (row["shno"] != null && row["shno"].ToString().Trim()!="")
                {
                    model.shno = row["shno"].ToString().Trim();
                }
                if (row["flwt"] != null)
                {
                    model.flwt = decimal.Parse(row["flwt"].ToString().Trim());
                }
                if (row["shwt"] != null)
                {
                    model.shwt = decimal.Parse(row["shwt"].ToString().Trim());
                }
                if (row["remark"] != null && row["remark"].ToString() != "")
                {
                    model.remark = row["remark"].ToString();
                }

            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,cjoid,wrlno,instoragetime,outid,outno,Operator,InDepart,OutDepart,status,islock,instotal,loadnumid,mechanerr,vloadnum,clearingmonth,flno,shno,flwt,shwt,remark ");
            strSql.Append(" FROM InStorage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,cjoid,wrlno,instoragetime,outid,outno,Operator,InDepart,OutDepart,status,islock,instotal,loadnumid,mechanerr,vloadnum,clearingmonth,flno,shno,flwt,shwt,remark ");
            strSql.Append(" FROM InStorage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM InStorage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from InStorage T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "InStorage";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod

  
    }
}

